# -*- coding: utf-8 -*-
# 数据库创建脚本
from migrate.versioning import api
from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO
from app import db
from app.models import User, ReviewSetting
import os
from hashlib import md5

__author__ = 'nibo'
# 删除数据库表
db.drop_all()
# 创建数据库表
db.create_all()

if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
    api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
    api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
# else:
#     api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO,
#                         api.version(SQLALCHEMY_MIGRATE_REPO))


# ========================== 初始化数据 ============================

email = 'xxxx'
password = 'xxxxx'

user = User(email=email, password=md5(email + password).hexdigest())
db.session.add(user)
db.session.commit()
print u'添加用户成功'

review_set = ReviewSetting(time=1, day=1)
db.session.add(review_set)
review_set = ReviewSetting(time=2, day=2)
db.session.add(review_set)
review_set = ReviewSetting(time=3, day=4)
db.session.add(review_set)
review_set = ReviewSetting(time=4, day=7)
db.session.add(review_set)
review_set = ReviewSetting(time=5, day=15)
db.session.add(review_set)
db.session.commit()
print u'添加复习设置参数数据成功'
# ===============================END===============================
